<?php
namespace jy_peisong;
defined('BY_JYA') or exit('error');
/*
  配送订单
	
*/
class order extends \plugin{
	/*
		抢单通知
	*/
	public function order_qiang($order){
		$order_id=intval($order['id']);
		$allusers=array();
		if($order['storeid'] && !$order['merchid']){
			$areas=pdo_getall('jy_peisong_area',array('uniacid'=>$_SESSION['uniacid'],'store_id OR'=>'store_id=0 or store_id = \''.$order['storeid'].'\''));
			foreach($areas as &$item){
				$users=pdo_getall('jy_peisong_member',array('online'=>1,'uniacid'=>$_SESSION['uniacid'],'group_id'=>$item['id'],'status'=>1),'mid');
				foreach($users as $uv){
					$allusers[]=$uv;
				}
			}
		}else{
			$users=pdo_getall('jy_peisong_member',array('online'=>1,'status'=>1,'uniacid'=>$_SESSION['uniacid']),'mid');
			foreach($users as $uv){
					$allusers[]=$uv;
			}
		}
		$nums=count($allusers);
		$key=rand(0,$nums-1);
		$key=$key?:0;
		$peisong_id=$allusers[$key]['mid'];
		if(!$peisong_id){
			//return false;
		}
		//$params=array('order_id'=>$order_id,'peisong_id'=>$peisong_id);
		$this->model('notice')->notice_ordermsg($order,$allusers);
		return true;
	}
	/*
		系统指派，peisong_id是MID
	*/
	public function order_pai($order){
		$order_id=intval($order['id']);
		$allusers=array();
		if($order['storeid'] && !$order['merchid']){
			$areas=pdo_getall('jy_peisong_area',array('uniacid'=>$_SESSION['uniacid'],'store_id OR'=>'store_id=0 or store_id = \''.$order['storeid'].'\''));
			foreach($areas as &$item){
				$users=pdo_getall('jy_peisong_member',array('group_id'=>$item['id'],'status'=>1),'mid');
				foreach($users as $uv){
					$allusers[]=$uv;
				}
			}
		}else{
			$users=pdo_getall('jy_peisong_member',array('status'=>1,'uniacid'=>$_SESSION['uniacid']),'mid');
			foreach($users as $uv){
					$allusers[]=$uv;
			}
		}
		$count=count($allusers)-1;
		if($count>=0){
			$key=rand(0,$count);
			$mid=$allusers[$key]['mid'];
			if($mid){
				$params=array('order_id'=>$order_id,'peisong_id'=>$mid);
				$this->order_sendget($params);
			}
		}
		return $mid;
	}
	/*
		抢单peisong_id是MID
	*/
	public function order_get($params){
		$order_id=intval($params['order_id']);
		$mid=intval($params['peisong_id']);
		//兼容错误，传过来的是ID而不是MID的时候！
		$mid=pdo_getcolumn('jy_peisong_member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),'mid');
		if(!$mid){
			$mid=pdo_getcolumn('jy_peisong_member',array('id'=>$mid),'mid');
		}
		$isget=pdo_getcolumn('jy_weishop_order',array('id'=>$order_id),'status');
		if($isget==2){
			exi('该订单已被别人抢单了！','error');
		}
		pdo_update('jy_weishop_order',array('peisong_id'=>$mid,'status'=>2,'send_status'=>1),array('id'=>$order_id));
		$this->create_sendlog($order_id,$mid,1);
		$this->model('notice')->notice_orderget($order_id);
		model('jy_weishop/order')->order_send($order_id,$params,true);
		return true;
	}
	/*
		发送订单给配送员
	*/
	public function order_sendget($params){
		$order_id=intval($params['order_id']);
		$mid=intval($params['peisong_id'])?:intval($params['paisong_id']);
		//兼容错误，传过来的是ID而不是MID的时候！
		$mid=pdo_getcolumn('jy_peisong_member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),'mid');
		if(!$mid){
			$mid=pdo_getcolumn('jy_peisong_member',array('id'=>$mid,'uniacid'=>$_SESSION['uniacid']),'mid');
		}
		pdo_update('jy_weishop_order',array('peisong_id'=>$mid,'status'=>2,'send_status'=>13),array('id'=>$order_id));
		$this->create_sendlog($order_id,$mid);
		$this->model('notice')->notice_orderget($order_id);
		return true;
	}
	/*
		配送员确定接单
	*/
	public function order_checkget($order_id){
		pdo_update('jy_weishop_order',array('peisong_id'=>$_SESSION['uid'],'status'=>2,'send_status'=>1),array('id'=>$order_id));
		$this->up_sendlog($order_id,$_SESSION['uid'],2);
		$this->model('notice')->notice_orderget($order_id);
		model('jy_weishop/order')->order_send($order_id,'',true);
		return true;
	}
	/*
		转单超时
	*/
	public function order_checktime($order_id=0){
		$setting=$this->plugin_setting('setting');
		$send_status13_time=intval($setting['send_status13_time']);
		if(!$send_status13_time){
			return false;
		}
		if($order_id){
			$sendlog=pdo_get('jy_peisong_sendlog',array('status'=>0,'mid'=>$_SESSION['uid'],'order_id'=>$order_id));
			if($sendlog['createtime']){
				$lasttime=$sendlog['createtime']+$send_status13_time*60;
				if($lasttime<TIMESTAMP){
					pdo_update('jy_weishop_order',array('peisong_id'=>0,'status'=>1,'send_status'=>12),array('id'=>$order_id));
					$this->up_sendlog($order_id,$sendlog['mid'],3);
				}
			}
		}else{
			$sendlogs=pdo_getall('jy_peisong_sendlog',array('status'=>0,'uniacid'=>$_SESSION['uniacid']));
			foreach($sendlogs as $sendlog){
				$order_id=$sendlog['order_id'];
				if($sendlog['createtime']){
					$setting=$this->plugin_setting('setting');
					$lasttime=$sendlog['createtime']+$send_status13_time*60;
					if($lasttime<TIMESTAMP){
						pdo_update('jy_weishop_order',array('peisong_id'=>0,'status'=>1,'send_status'=>12),array('id'=>$order_id));
						$this->up_sendlog($order_id,$sendlog['mid'],3);
					}
				}
			}
		}
		return true;
	}
	/*
		完成订单
	*/
	public function order_receive($order_id){
		$this->create_orderlog($order_id);
		pdo_update('jy_weishop_order',array('send_status'=>4),array('id'=>$order_id));
		model('jy_weishop/order')->order_receive($order_id);
		$this->model('order')->up_sendlog($order_id,'',2);
		return true;
	}
	/*
		接单日志
	*/
	public function create_sendlog($order_id,$mid=0,$status=0){
		$mid=$mid?:$_SESSION['uid'];
		$this->pdo_update('sendlog',array('status'=>3),array('order_id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
		$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$mid,
				'order_id'=>$order_id,
				'status'=>$status,
				'createtime'=>TIMESTAMP,
		);
		$this->insert('sendlog',$data);
		return true;
	}
	/*
		uplog
	*/
	public function up_sendlog($order_id,$mid=0,$status=0){
		$mid=$mid?:$_SESSION['uid'];
		$this->pdo_update('sendlog',array('status'=>$status),array('mid'=>$mid,'order_id'=>$order_id,'status <>'=>3,'uniacid'=>$_SESSION['uniacid']));
		return true;
	}
	/*
		创建结算订单
	*/
	function create_orderlog($order_id,$mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$order=pdo_get('jy_weishop_order',array('id'=>$order_id),array('ordersn','price'));
		$orderlog=$this->pdo_get('orderlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid,'ordersn'=>$order['ordersn']),array('id'));
		if($orderlog['id']){
			exi('请勿重复提交！','error');
		}
		//按商品/按件
		if($this->setting['peisong_commission_type']){
			$get_price=$get_money=$this->setting['peisong_fee'];
		}else{
			$get_price=$get_money=$this->setting['peisong_fee'];
		}
		$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$mid,
				'ordersn'=>$order['ordersn'],
				'order_price'=>$order['price'],
				'get_price'=>$get_price,
				'get_money'=>$get_money,
				'createtime'=>TIMESTAMP,
				'status'=>0,
		);
		$this->insert('orderlog',$data);
		return true;
	}
	/*
		订单升级为待结算
	*/
	public function cash_orderlog(){
		$cashdays=$this->setting['cash_day']?:1;
		$cashtime = mktime(0,0,0,date('m'),date('d')-$cashdays,date('Y'));
		$orderlog=pdo_getall('jy_peisong_orderlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>0,'createtime <'=>$cashtime));
		foreach($orderlog as $item){
			pdo_update('jy_peisong_orderlog',array('status'=>1),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
		}
		return true;
	}
	/*
		待结算订单升级为结算完成/未结算
	*/
	public function change_orderlog($mid,$status=0){
		$orderlog=pdo_getall('jy_peisong_orderlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid,'status'=>1));
		foreach($orderlog as $item){
			pdo_update('jy_peisong_orderlog',array('status'=>1),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'status'=>$status));
		}
		return true;
	}
	/*
		审核结算单
	*/
	public function check_balance($balance_id,$status,$remark=''){
		$update=array(
			'status'=>$status,
			'remark'=>$remark,
		);
		$this->update('cashlog',$update,array('id'=>$balance_id));
	}
	/*
		打款结算单
	*/
	public function pay_balance($balance_id,$pay_type='',$wxgid='',$pay_trade_no=''){
		$balance=$this->get('cashlog',array('id'=>$balance_id,'uniacid'=>$_SESSION['uniacid']));
		if(!$balance || $balance['status']!=1){
			exi('结算单不存在或者不符合打款条件，打款失败！','error');
		}
		$pay_types=array_keys($this->pay_types);
		if(!$pay_type || !in_array($pay_type,$pay_types)){
			//exi('未设置打款方式或者打款方式不合法，打款失败！','error');
		}
		$pay_type=$this->pay_types2[$pay_type];
		$func_name='pay_balance_by_'.$pay_type;
		$trade_no=$this->$func_name($balance['mid'],$balance['getprice'],$balance['id'],$wxgid);
		$update=array(
			'status'=>3,
			'applysn'=>$trade_no=='pay_trade_no'?$pay_trade_no:$trade_no,
		);
		if($pay_type){
			$update['paytype']=$pay_type;
		}
		$this->update('cashlog',$update,array('id'=>$balance['id']));
		return true;
	}
	/*
		打款到余额
	*/
	protected function pay_balance_by_credit($mid,$money,$balancesn){
		return cfc('core')->member_credit($mid,'credit2',$money,true,$this->plugin_name,$remark='分销佣金打款');
	}
	/*
		打款到微信零钱
	*/
	protected function pay_balance_by_wechat($mid,$money,$balancesn,$wxgid=''){
		if($wxgid){
			$wxapp=pdo_get('core_account_wxapp',array('id'=>$wxgid,'uniacid'=>$_SESSION['uniacid']),array('appid','mchid','paykey'));
		}
		$params=array(
			'appid'=>$wxapp['appid']?:'',
			'mch_id'=>$wxapp['mchid']?:'',
			'openid'=>$mid,
			'mch_billno'=>$balancesn,
			'money'=>$money,
			'wxgid'=>$wxgid,
			'remark'=>'代理商佣金发放',
		);
		$res=cfc('wechat_pay')->mch_pay($params);
		return $res['payment_no'];
	}
	/*
		打款到支付宝
	*/
	protected function pay_balance_by_alipay($mid,$money,$balancesn){
		return 'pay_trade_no';
		exi('暂不支持打款到支付宝！','error');
	}
	/*
		打款到银行账户
	*/
	protected function pay_balance_by_union($mid,$money,$balancesn){
		return 'pay_trade_no';
		exi('暂不支持打款到银行账户！','error');
	}
}